package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.com.api.entity.ApiT_member;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/**
 * ClassName: ApiWalletController
 * 
 * @Description: 钱包控制器
 * @author yewl
 * @date 2017年4月26日
 */
@Controller
@RequestMapping(Utils.SYS_API + "wallet/")
public class ApiWalletController {

	@Autowired
	private ApiT_memberService memberService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiWalletController.class);

	/**
	 * 用户最近的转账人员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getnearlyTransferUser")
	public void getnearlyTransferUser(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				List<ApiT_member> list = memberService.getnearlyTransferUser(user_id, pageInfo);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: createtransferorder
	 * @Description: 生成转账订单
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @param touser_id
	 * @param createType
	 *            1-表示普通的转账，需要对方确认，0-扫码支付（转账），不需要对方确认
	 * @param money
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "createtransferorder")
	public void createtransferorder(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String touser_id, String createType, String money) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.createtransferorder(user_id, touser_id, createType, money);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: transferMoney
	 * @Description: 余额转账给某人
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @param touser_id
	 * @param pwd
	 * @param money
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "transferMoney")
	public void transferMoney(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String touser_id, String pwd, String money) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.transferMoney(user_id, touser_id, pwd, money);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 接受转账
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "receiveMoney")
	public void receiveMoney(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String record_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.receiveMoney(user_id, record_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 生成充值订单
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "depositorder")
	public void depositorder(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String money) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.depositorder(user_id, money);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 提现接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "withdrawCash")
	public void withdrawCash(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bankcard_id, String money, String pwd) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.withdrawCash(user_id, bankcard_id, money, pwd);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 生成付款二维码（先生成订单）
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createPayQRcode")
	public void createPayQRcode(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.createPayQRcode(user_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: scanReceiveMoney
	 * @Description: 扫码收款接口
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @param money
	 *            收款金额
	 * @param ordernum
	 *            收款码对应订单的订单编号
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "scanReceiveMoney")
	public void scanReceiveMoney(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String money, String ordernum) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.scanReceiveMoney(user_id, money, ordernum);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: checkPayQRCodeOrder
	 * @Description:检查付款码关联的订单的状态
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @param ordernum
	 *            收款码对应订单的订单编号
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "checkPayQRCodeOrder")
	public void checkPayQRCodeOrder(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String ordernum) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = memberService.checkPayQRCodeOrder(ordernum);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
